<template>
  <div>
    <!-- 数据表格 -->
    <el-table v-loading="globalConfigTableLoading" :data="globalOrgCustomSetting" class="gl_table_noBorder" style="width: 100%">
      <el-table-column width="40" />
      <el-table-column prop="date" label="赛事方的机构等级">
        <template slot-scope="scope">
          <span>{{ scope.row[0].unitTypeName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="需要审核的赛事认证等级" width="280">
        <template slot-scope="scope">
          <div v-for="item in scope.row" :key="item.id">
            <span>{{ item.competitionLevel.name }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="参与审核流的机构等级">
        <template slot-scope="scope">
          <div v-for="item in scope.row" :key="item.id">
            <el-checkbox v-model="item.isNeedProvinceApprove">省级</el-checkbox>
            <el-checkbox v-model="item.isNeedCityApprove" :disabled="!item.isCityApproveChangeable">市级</el-checkbox>
            <el-checkbox v-model="item.isNeedCountyApprove" :disabled="!item.isCountyApproveChangeable">县/区级</el-checkbox>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 操作按钮 -->
    <el-row class="gl_mt_15">
      <el-col :span="24" align="right">
        <!-- 查询按钮 -->
        <el-button type="primary" size="mini" :disabled="!(globalConfigTableLoading !== true && globalOrgCustomSetting.length !== 0)" @click="submitEditGlobalSettings">应用机构权限配置</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  api_GET_EventsManagementCompetitionApproveSettingGlobalSettings // 获取审核流程的全局配置
} from '@/extend/api/gameswaggerApi/app.js';

import {
  api_POST_EventsManagementCompetitionApproveSettingSetGlobalSettings // 新增或者修改全局设置
} from '@/extend/api/gameswaggerApi/app.js';

export default {
  props: {
    organizationUnitId: {
      type: String,
      default: ''
    },
    delegationId: {
      type: String,
      default: ''
    },
    organizationUnitsTypes: {
      type: Array,
      default () {
        return [];
      }
    }
  },
  data () {
    return {
      globalOrgCustomSetting: [],
      globalConfigTableLoading: false
    };
  },
  created () {
    this.getPageList();
  },
  methods: {
    // 获取全局配置
    getPageList () {
      this.globalConfigTableLoading = true;
      api_GET_EventsManagementCompetitionApproveSettingGlobalSettings()
        .then(
          res => {
            this.globalConfigTableLoading = false;
            // 聚合数据
            const _returnList = [];
            let _list = [];
            let _unitType = '';
            for (let i = 0; i < res.length; i++) {
              const item = res[i];
              if (i !== 0 && item.unitType !== _unitType) {
                _returnList.push(_list.slice(0));
                _list = [];
              }

              _list.push(item);
              _unitType = item.unitType;

              if (i === res.length - 1) {
                _returnList.push(_list.slice(0));
              }
            }
            // 赋值聚合后的数据
            this.globalOrgCustomSetting = _returnList;
          }
        )
        .catch(
          error => {
            this.globalConfigTableLoading = false;
            this.globalOrgCustomSetting = [];
            this.$message({
              message: `列表获取失败，${error.error.message}`,
              type: 'error',
              showClose: true
            });
          }
        );
    },
    /* 确定新建或修改全局设置 */
    submitEditGlobalSettings (list) {
      const _vo = {
        competitionApproveSettings: []
      };
      this.globalOrgCustomSetting.forEach(item => {
        _vo.competitionApproveSettings = _vo.competitionApproveSettings.concat(item);
      });
      this.editGlobalSettingApi(_vo);
    },
    editGlobalSettingApi (vo) {
      this.globalConfigTableLoading = true;
      api_POST_EventsManagementCompetitionApproveSettingSetGlobalSettings(vo)
        .then(
          res => {
            this.$message({
              message: `编辑成功！`,
              type: 'success',
              showClose: true
            });
            this.getPageList();
            this.globalConfigTableLoading = false;
          }
        )
        .catch(
          error => {
            this.globalConfigTableLoading = false;
            this.$message({
              message: `编辑失败，${error.error.message}`,
              type: 'error',
              showClose: true
            });
          }
        );
    }
  }
};
</script>

<style lang="scss" scoped>

</style>
